package 力扣;

/**
 给定一个二叉树，检查它是否是镜像对称的。

 例如，二叉树 [1,2,2,3,4,4,3] 是对称的。

    1
   / \
  2   2
 / \ / \3  4 4  3
  

 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

 1
 / \
 2   2
 \   \
 3    3
 */
public class _101对称二叉树 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        if( root ==null ) return  true;
        return isSameTree(root.left, root.right);
    }

    private boolean isSameTree(TreeNode left, TreeNode right) {
        if(left == null && right == null ) return true;
        if(left == null || right == null ) return false;
        if( left.val != right.val) return false;
        return isSameTree(left.left,right.right) && isSameTree(left.right,right.left);
    }
}
